package com.teach;

public class Demo06 implements Runnable {

	public static Product p = new Product();

	public static void main(String[] args) {
//		for (int i = 0; i < 100; i++) {
//			System.out.println(p.next());
//		}
		for (int i = 0; i < 100; i++) {
			new Thread(new Demo06()).start();
		}

	}
	
	@Override
	public void run() {
		for(int i=0;i<1000;i++){
			synchronized(p) {//同步块
				System.out.println(Thread.currentThread().getName()+"\t"+p.next());
			}
		}
	}

	private static class Product {
		private Long i = 0l;

//		public synchronized Long next() {//同步方法
		public  Long next() {
			i = i + 1;
			return i;
		}
	}

}
